package com.woniuxy.amusementparkos.service.xxy.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.woniuxy.amusementparkos.dao.xxy.StoreDao;
import com.woniuxy.amusementparkos.dao.xxy.User_StoreDao;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.entity.xxy.User_Store;
import com.woniuxy.amusementparkos.exception.xxy.DeleteUserException;
import com.woniuxy.amusementparkos.exception.xxy.RegistException;
import com.woniuxy.amusementparkos.exception.xxy.LoginException;
import com.woniuxy.amusementparkos.exception.xxy.UpdatePasswordException;
import com.woniuxy.amusementparkos.service.xxy.Store_UserService;
@Service
public class Store_UserServiceImpl implements Store_UserService {
	@Autowired
	User_StoreDao userDao;
	//数据库中的用户
	User_Store user_in;
	Store_Info store_in;
	/**
	 * 注册
	 */
	@Override
	public void regist(User_Store user) {
		try {
		 store_in = userDao.selectStoreByKey(user.getStore_Info().getStore_key());
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(store_in==null||store_in.getStore_id()!=user.getStore_Info().getStore_id()) throw new RegistException("新增失败,您输入的商家码有误");
		try {
			user_in = userDao.selectUser_StoreByUser_Id(user.getUser_id());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegistException("系统错误");
		}
		if(user_in!=null) throw new RegistException("用户名已存在");
		try {
			String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
			user.setPassword(password);
			userDao.insertUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegistException("系统错误");
		}
	}
	/**
	 * 密码修改
	 */
	@Override
	public void updatePassword(int user_id, String password_old, String password_new) {
		User_Store user;
		password_old = DigestUtils.md5DigestAsHex(password_old.getBytes());
		password_new = DigestUtils.md5DigestAsHex(password_new.getBytes());
		try {
			user = userDao.selectUser_StoreByUser_Id(user_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegistException("系统错误");
		}
		if(!user.getPassword().equals(password_old))throw new UpdatePasswordException("修改失败,你输入的原来的密码不正确");
		try {
			user.setPassword(password_new);
			userDao.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RegistException("系统错误");
		}
	}
	
	/**
	 * 用key登陆
	 */
	@Override
	public Store_Info loginByKey(String key) {
		Store_Info store;
		try {
			store = userDao.selectStoreByKey(key);
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
		if(store==null)throw new LoginException("您输入的商家码不存在");
		return store;
	}
	/**
	 * 通过店铺id查该商店的user
	 */
	@Override
	public List<User_Store> selectUser_StreByStore_Id(int store_id) {
		try {
			List<User_Store> users = userDao.selectUser_StreByStore_Id(store_id);
			return users;
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
		
	}
	@Override
	public long selectUser_StreByStore_IdCount(int store_id) {
		try {
			long count = userDao.selectUser_StreByStore_IdCount(store_id);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
	}
	@Override
	public void deleteUser(String user_id, String store_key) {
		Store_Info storeByKey;
		Store_Info storeByUserId;
		try {
			storeByKey = userDao.selectStoreByKey(store_key);
			storeByUserId = userDao.selectStoreByUserId(Integer.parseInt(user_id));
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
		if(storeByKey==null)throw new DeleteUserException("抱歉您输入的商家码不存在,删除失败");
		if(storeByKey.getStore_id()!=storeByUserId.getStore_id()) throw new DeleteUserException("删除失败,您输入的商家码不正确");
		
		try {
			userDao.deleteUserByUserId(Integer.parseInt(user_id));
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
	}
	/**
	 * 登陆
	 * 
	 */
	@Override
	public User_Store login(User_Store user) {
		String pwd = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		try {
			 user_in = userDao.selectUser_StoreByUser_Id(user.getUser_id());
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
		if(user_in==null)throw new LoginException("用户名不存在");
		if(!user_in.getPassword().equals(pwd)) throw new LoginException("密码错误");
		try {
			userDao.updateLastLoginTime(user_in.getUser_id());
		} catch (Exception e) {
			e.printStackTrace();
			throw new LoginException("系统错误,请联系管理员");
		}
		return user_in;
	}

	

}
